Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
p-cancelable
Advanced tools
The p-cancelable package provides a way to create cancelable promises in JavaScript. This is particularly useful for managing asynchronous operations that may need to be canceled before they complete, such as HTTP requests, long-running computations, or any other task that might not need to continue executing if certain conditions are met.
Creating a cancelable promise
This feature allows the creation of a new cancelable promise. The promise can be canceled at any time before it resolves or rejects, which triggers the onCancel function where cleanup operations like clearing timeouts or aborting requests can be performed.
const PCancelable = require('p-cancelable');
const myCancelablePromise = new PCancelable((resolve, reject, onCancel) => {
const timer = setTimeout(() => {
resolve('Done!');
}, 2000);
onCancel(() => {
clearTimeout(timer);
reject(new Error('Operation canceled.'));
});
});
myCancelablePromise.cancel();
Bluebird is a comprehensive promise library that includes support for cancelable promises. Compared to p-cancelable, Bluebird offers a broader set of promise-related utilities, such as map, reduce, and filter for promises, making it suitable for more complex scenarios.
The abort-controller package provides a way to abort one or more Web API tasks, such as Fetch API requests, by using an AbortSignal. While it serves a similar purpose for canceling tasks, it is specifically designed to work with the Fetch API and other APIs that support aborting, unlike p-cancelable which is more generic.
Create a promise that can be canceled
Useful for animation, loading resources, long-running async computations, async iteration, etc.
$ npm install --save p-cancelable
const PCancelable = require('p-cancelable');
const cancelablePromise = new PCancelable((onCancel, resolve, reject) => {
const worker = new SomeLongRunningOperation();
onCancel(() => {
worker.close();
});
worker.on('finish', resolve);
worker.on('error', reject);
});
cancelablePromise
.then(value => {
console.log('Operation finished successfully:', value);
})
.catch(reason => {
if (cancelablePromise.canceled) {
// Handle the cancelation here
console.log('Operation was canceled');
return;
}
throw reason;
});
// Cancel the operation after 10 seconds
setTimeout(() => {
cancelablePromise.cancel();
}, 10000);
Same as the Promise
constructor, but with a prepended onCancel
parameter in executor
.
PCancelable
is a subclass of Promise
.
Type: Function
Accepts a function that is called when the promise is canceled.
You're not required to call this function.
Type: Function
Cancel the promise. The cancellation is synchronous.
Calling it after the promise has settled or multiple times does nothing.
Type: boolean
Whether the promise is canceled.
Type: Error
Rejection reason when .cancel()
is called.
Convenience method to make your promise-returning or async function cancelable.
The function you specify will have onCancel
prepended to its parameters.
const fn = PCancelable.fn((onCancel, input) => {
const job = new Job();
onCancel(() => {
job.cleanup();
});
return job.start(); //=> Promise
});
const promise = fn('input'); //=> PCancelable
// …
promise.cancel();
In American English, the verb cancel is usually inflected canceled and canceling—with one l.
Both a browser API and the Cancelable Promises proposal use this spelling.
It's still an early draft and I don't really like its current direction. It complicates everything and will require deep changes in the ecosystem to adapt to it. And the way you have to use cancel tokens is verbose and convoluted. I much prefer the more pragmatic and less invasive approach in this module. The proposal was withdrawn.
MIT © Sindre Sorhus
FAQs
Create a promise that can be canceled
The npm package p-cancelable receives a total of 18,571,890 weekly downloads. As such, p-cancelable popularity was classified as popular.
We found that p-cancelable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.